package com.hjqjl.whdemo.feature.feature0.ui.activity.custom;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import androidx.appcompat.app.AlertDialog;

import com.hjqjl.whdemo.R;
import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.databinding.ActivityDialogfragmentBinding;
import com.hjqjl.whdemo.dialog.dialogfragment.DialogAlertFragment;
import com.hjqjl.whdemo.dialog.dialogfragment.DialogBottomFragment;
import com.hjqjl.whdemo.dialog.dialogfragment.DialogCustomFragment;
import com.hjqjl.whdemo.dialog.dialogfragment.DialogLoadingFragment;
import com.hjqjl.whdemo.dialog.dialogfragment.DialogTopFragment;

public class DialogFragmentActivity extends BaseActivity {
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, DialogFragmentActivity.class);
        context.startActivity(intent);
    }

    private ActivityDialogfragmentBinding binding;

    @Override
    protected void initData() {
        binding = ActivityDialogfragmentBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
    }

    @Override
    protected void processLogic() {
        binding.btnTopdialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogTopFragment dialogTopFragment = DialogTopFragment.newInstance();
                //show要比showNow稍微“慢”一点，这导致调用show了后，立刻修改dialog中的view（例如textView修改字符内容）会崩溃，而showNow不会
                dialogTopFragment.showNow(getSupportFragmentManager(), "topDialog");
            }
        });

        binding.btnCustomdialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogCustomFragment dialogCustomFragment =
                        DialogCustomFragment.newInstance("我是title", "我是内容--我是自定义的DialogFragment");
                dialogCustomFragment.setCallBack(new DialogCustomFragment.ClickCallBack() {
                    @Override
                    public void clickCancel() {
                        com.blankj.utilcode.util.ToastUtils.showShort("取消--我是自定义的DialogFragment");
                    }

                    @Override
                    public void clickConfirm() {
                        com.blankj.utilcode.util.ToastUtils.showShort("确定--我是自定义的DialogFragment");
                    }
                });
                dialogCustomFragment.showNow(getSupportFragmentManager(), "customDialog");
            }
        });

        binding.btnDialogLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogLoadingFragment dialogLoadingFragment = DialogLoadingFragment.newInstance("加载中...");
                dialogLoadingFragment.showNow(getSupportFragmentManager(), "DialogLoading");
            }
        });

        binding.btnBottomdialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogBottomFragment dialogBottomFragment = DialogBottomFragment.newInstance();
                dialogBottomFragment.showNow(getSupportFragmentManager(), "BottomDialog");
            }
        });

        binding.btnAlert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogAlertFragment alertDialogFragment = DialogAlertFragment.newInstance
                        ("系统弹窗AlertDialog", "我是内容--MyAlertDialogFragment", "确定", "取消"
                                , new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        com.blankj.utilcode.util.ToastUtils.showShort("我是内容--MyAlertDialogFragment");
                                    }
                                }
                                , null);
                alertDialogFragment.showNow(getSupportFragmentManager(), "alertDialog");
            }
        });

        binding.btnAlertDialog.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("系统弹窗AlertDialog");
            builder.setMessage("Message这是一个普通对话框");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setCancelable(true);            //点击对话框以外的区域是否让对话框消失
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog dialog = builder.create();//创建AlertDialog对象
            dialog.show();
        });
    }
}
